layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 销售管理管理
     */
    var Sale = {
        tableId: "saleTable"
    };

    /**
     * 初始化表格的列
     */
    Sale.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'saleId', sort: true, title: '销售码'},
            {field: 'remark', sort: true, title: '销售备注'},
            {field: 'orderId', sort: true, title: '出货单编码'},
            {field: 'worker', sort: true, title: '销售人员'},
            {field: 'client', sort: true, title: '客户'},
            {field: 'payWay', sort: true, title: '付款方式', templet:function (d) {
                    if (d.payWay === 0) {
                        return  '<span class="layui-badge">现金</span>';
                    } else if (d.payWay === 1) {
                        return  '<span class="layui-badge layui-bg-cyan">月结</span>';
                    } else {
                        return  '<span class="layui-badge layui-bg-cyan">未知</span>';
                    }
                }},
            {field: 'updateTime', sort: true, title: '创建时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Sale.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(Sale.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    Sale.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/api/sale/edit';
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    Sale.openEditDlg = function (data) {
        if (data!=null) {
            window.location.href = Feng.ctxPath + '/api/sale/edit?saleId=' + data.saleId;

        } else  {
            window.location.href = Feng.ctxPath + '/api/sale/edit';

        }

    };

    /**
     * 导出excel按钮
     */
    Sale.exportExcel = function () {
        var checkRows = table.checkStatus(Sale.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Sale.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/api/sale/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Sale.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("saleId", data.saleId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Sale.tableId,
        url: Feng.ctxPath + '/api/sale/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Sale.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Sale.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Sale.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Sale.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Sale.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Sale.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Sale.onDeleteItem(data);
        }
    });
});
